package com.cencat.driver.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.cencat.common.entity.BaseEntity;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * 车辆实体类
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("vehicle")
public class Vehicle extends BaseEntity {

    /**
     * 车辆ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 车辆编码
     */
    @NotBlank(message = "车辆编码不能为空")
    @Size(max = 32, message = "车辆编码长度不能超过32个字符")
    @TableField("vehicle_code")
    private String vehicleCode;

    /**
     * 车牌号
     */
    @NotBlank(message = "车牌号不能为空")
    @Pattern(regexp = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]$", message = "车牌号格式不正确")
    @TableField("license_plate")
    private String licensePlate;

    /**
     * 车辆类型：1-小型货车，2-中型货车，3-大型货车，4-电动车，5-摩托车
     */
    @NotNull(message = "车辆类型不能为空")
    @Min(value = 1, message = "车辆类型值不能小于1")
    @Max(value = 5, message = "车辆类型值不能大于5")
    @TableField("vehicle_type")
    private Integer vehicleType;

    /**
     * 品牌
     */
    @Size(max = 50, message = "品牌长度不能超过50个字符")
    @TableField("brand")
    private String brand;

    /**
     * 型号
     */
    @Size(max = 50, message = "型号长度不能超过50个字符")
    @TableField("model")
    private String model;

    /**
     * 颜色
     */
    @Size(max = 20, message = "颜色长度不能超过20个字符")
    @TableField("color")
    private String color;

    /**
     * 发动机号
     */
    @Size(max = 50, message = "发动机号长度不能超过50个字符")
    @TableField("engine_no")
    private String engineNo;

    /**
     * 车架号
     */
    @Size(max = 50, message = "车架号长度不能超过50个字符")
    @TableField("vin")
    private String vin;

    /**
     * 载重量（吨）
     */
    @DecimalMin(value = "0.0", message = "载重量不能小于0")
    @TableField("load_capacity")
    private BigDecimal loadCapacity;

    /**
     * 载货体积（立方米）
     */
    @DecimalMin(value = "0.0", message = "载货体积不能小于0")
    @TableField("volume_capacity")
    private BigDecimal volumeCapacity;

    /**
     * 燃料类型：1-汽油，2-柴油，3-电动，4-混合动力
     */
    @Min(value = 1, message = "燃料类型值不能小于1")
    @Max(value = 4, message = "燃料类型值不能大于4")
    @TableField("fuel_type")
    private Integer fuelType;

    /**
     * 购买日期
     */
    @TableField("purchase_date")
    private LocalDate purchaseDate;

    /**
     * 保险到期日期
     */
    @TableField("insurance_expire_date")
    private LocalDate insuranceExpireDate;

    /**
     * 年检日期
     */
    @TableField("annual_inspection_date")
    private LocalDate annualInspectionDate;

    /**
     * 状态：0-停用，1-正常，2-维修中，3-报废
     */
    @Min(value = 0, message = "状态值不能小于0")
    @Max(value = 3, message = "状态值不能大于3")
    @TableField("status")
    private Integer status;

    /**
     * 当前司机ID
     */
    @TableField("current_driver_id")
    private Long currentDriverId;

    /**
     * 总里程（公里）
     */
    @DecimalMin(value = "0.0", message = "总里程不能小于0")
    @TableField("mileage")
    private BigDecimal mileage;

    /**
     * 油耗（升/百公里）
     */
    @DecimalMin(value = "0.0", message = "油耗不能小于0")
    @TableField("fuel_consumption")
    private BigDecimal fuelConsumption;

    /**
     * 保养里程
     */
    @DecimalMin(value = "0.0", message = "保养里程不能小于0")
    @TableField("maintenance_mileage")
    private BigDecimal maintenanceMileage;

    /**
     * 下次保养里程
     */
    @DecimalMin(value = "0.0", message = "下次保养里程不能小于0")
    @TableField("next_maintenance_mileage")
    private BigDecimal nextMaintenanceMileage;

    /**
     * 车辆图片（JSON数组）
     */
    @TableField("images")
    private String images;

    /**
     * 相关证件（JSON数组）
     */
    @TableField("documents")
    private String documents;

    /**
     * 租户ID
     */
    @NotNull(message = "租户ID不能为空")
    @TableField("tenant_id")
    private Long tenantId;

    // 车辆类型枚举常量
    public static final int TYPE_SMALL_TRUCK = 1;   // 小型货车
    public static final int TYPE_MEDIUM_TRUCK = 2;  // 中型货车
    public static final int TYPE_LARGE_TRUCK = 3;   // 大型货车
    public static final int TYPE_ELECTRIC = 4;      // 电动车
    public static final int TYPE_MOTORCYCLE = 5;    // 摩托车

    // 燃料类型枚举常量
    public static final int FUEL_GASOLINE = 1;      // 汽油
    public static final int FUEL_DIESEL = 2;        // 柴油
    public static final int FUEL_ELECTRIC = 3;      // 电动
    public static final int FUEL_HYBRID = 4;        // 混合动力

    // 状态枚举常量
    public static final int STATUS_DISABLED = 0;    // 停用
    public static final int STATUS_NORMAL = 1;      // 正常
    public static final int STATUS_MAINTENANCE = 2; // 维修中
    public static final int STATUS_SCRAPPED = 3;    // 报废

    /**
     * 获取车辆类型描述
     */
    public String getVehicleTypeDesc() {
        switch (this.vehicleType) {
            case TYPE_SMALL_TRUCK: return "小型货车";
            case TYPE_MEDIUM_TRUCK: return "中型货车";
            case TYPE_LARGE_TRUCK: return "大型货车";
            case TYPE_ELECTRIC: return "电动车";
            case TYPE_MOTORCYCLE: return "摩托车";
            default: return "未知";
        }
    }

    /**
     * 获取燃料类型描述
     */
    public String getFuelTypeDesc() {
        switch (this.fuelType) {
            case FUEL_GASOLINE: return "汽油";
            case FUEL_DIESEL: return "柴油";
            case FUEL_ELECTRIC: return "电动";
            case FUEL_HYBRID: return "混合动力";
            default: return "未知";
        }
    }

    /**
     * 获取状态描述
     */
    public String getStatusDesc() {
        switch (this.status) {
            case STATUS_DISABLED: return "停用";
            case STATUS_NORMAL: return "正常";
            case STATUS_MAINTENANCE: return "维修中";
            case STATUS_SCRAPPED: return "报废";
            default: return "未知";
        }
    }

    /**
     * 判断是否可用
     */
    public boolean isAvailable() {
        return STATUS_NORMAL == this.status;
    }

    /**
     * 判断是否需要保养
     */
    public boolean needsMaintenance() {
        if (mileage == null || nextMaintenanceMileage == null) {
            return false;
        }
        return mileage.compareTo(nextMaintenanceMileage) >= 0;
    }

    /**
     * 判断保险是否即将到期（30天内）
     */
    public boolean isInsuranceExpiringSoon() {
        if (insuranceExpireDate == null) {
            return false;
        }
        return insuranceExpireDate.isBefore(LocalDate.now().plusDays(30));
    }

    /**
     * 判断年检是否即将到期（30天内）
     */
    public boolean isInspectionExpiringSoon() {
        if (annualInspectionDate == null) {
            return false;
        }
        return annualInspectionDate.isBefore(LocalDate.now().plusDays(30));
    }
}